Socket
Socket
Sign inDemoInstall

@floating-ui/react-dom

Package Overview
Dependencies
8
Maintainers
2
Versions
44
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @floating-ui/react-dom

Floating UI for React DOM


Version published
Weekly downloads
9.3M
increased by3.86%
Maintainers
2
Created
Weekly downloads
 

Package description

What is @floating-ui/react-dom?

The @floating-ui/react-dom package is a library for creating floating elements that can be positioned around a target element in a React application. It provides a robust API for positioning tooltips, popovers, dropdowns, and other floating elements, with extensive control over placement, flipping, and shifting based on available space in the viewport.

What are @floating-ui/react-dom's main functionalities?

Positioning Tooltips

This code demonstrates how to create a simple tooltip that appears above a button when clicked. It uses the useFloating hook from @floating-ui/react-dom to manage the tooltip's position and behavior, including automatic adjustment for viewport boundaries.

import { useFloating, offset, flip, shift, arrow } from '@floating-ui/react-dom';
import { useState, useRef } from 'react';

function Tooltip() {
  const [open, setOpen] = useState(false);
  const arrowRef = useRef(null);
  const {x, y, reference, floating, strategy} = useFloating({
    placement: 'top',
    middleware: [offset(5), flip(), shift({padding: 5}), arrow({element: arrowRef})]
  });

  return (
    <>
      <button ref={reference} onClick={() => setOpen(!open)}>
        Hover me
      </button>
      {open && (
        <div ref={floating} style={{position: strategy, top: y ?? '', left: x ?? ''}}>
          Tooltip content
          <div ref={arrowRef} />
        </div>
      )}
    </>
  );
}

Creating Popovers

This example shows how to create a popover that appears to the right of a button when clicked. The useFloating hook is used to handle dynamic positioning and flipping to ensure the popover remains visible within the viewport.

import { useFloating, offset, flip, shift } from '@floating-ui/react-dom';
import { useState, useRef } from 'react';

function Popover() {
  const [open, setOpen] = useState(false);
  const {x, y, reference, floating, strategy} = useFloating({
    placement: 'right-start',
    middleware: [offset(10), flip(), shift({padding: 8})]
  });

  return (
    <>
      <button ref={reference} onClick={() => setOpen(!open)}>
        Click me
      </button>
      {open && (
        <div ref={floating} style={{position: strategy, top: y ?? '', left: x ?? ''}}>
          Popover content
        </div>
      )}
    </>
  );
}

Other packages similar to @floating-ui/react-dom

Readme

Source

@floating-ui/react-dom

This is the library to use Floating UI with React DOM.

Keywords

FAQs

Last updated on 12 Feb 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc